<template>
  <div class="page_menu app-container" v-loading="loading">
    <cfCard title='' class='page-container-search bottom12'>
      <div slot='content'>
        <el-form :model="searchParams" ref="searchParams" size="small" :inline="true">
          <el-form-item label="菜单名称" prop="menuName">
            <el-input v-model="searchParams.menuName" placeholder="请输入菜单名称" clearable v-search="handleSearch"/>
          </el-form-item>
          <el-form-item label="状 态" prop="status">
            <el-select v-model="searchParams.status" placeholder="菜单状态" clearable @change="handleSearch">
              <el-option v-for="dict in sysNormalDisable" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"/>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleSearch">查询</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="handleReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </cfCard>
    <cfCard title='菜单列表' hasTable :tableObj='tableDataObj' class='page_component-container-top'>
      <template slot="btn">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:menu:add']">新增</el-button>
        <el-button type="info" plain icon="el-icon-sort" size="mini" @click="toggleExpandAll">展开/折叠</el-button>
      </template>
      <div slot='content'>
        <cfTable :tableDataObj='tableDataObj' v-if="refreshTable" :tableData='tableData'>
          <template slot='icon'>
            <el-table-column label='图 标' width='100px'>
              <svg-icon slot-scope="scope" :icon-class="scope.row.icon" />
            </el-table-column>
          </template>
          <template slot='status'>
            <el-table-column label='状 态' width='100px'>
              <dict-tag slot-scope="scope" :options="sysNormalDisable" :value="scope.row.status"/>
            </el-table-column>
          </template>
          <template slot='operate'>
            <el-table-column label='操 作' width='140px' align='right'>
              <span slot-scope='{row}'>
                <el-button type='text' size='small' v-hasPermi="['system:menu:add']" @click='handleAdd(row)'>新增</el-button>
                <el-button type='text' size='small' @click='handleEdit(row)'>编辑</el-button>
                <el-button type='text' class="delete-span" size='small' @click='handleDelete(row)'>删除</el-button>
              </span>
            </el-table-column>
          </template>
        </cfTable>
      </div>
    </cfCard>
    <updateDialog v-if="dialogObj.visible" :dialogObj="dialogObj" @refresh="getListFn"></updateDialog>
  </div>
</template>

<script>
import { listMenu, getMenu, delMenu, addMenu, updateMenu } from "@/api/system/menu";
import updateDialog from "./updateDialog.vue";

export default {
  name: "Menu",
  components: {updateDialog},
  data() {
    return {
      loading: true,/**遮罩层*/ 
      tableData: [],/**菜单表格树数据*/ 
      dialogObj: {
        visible: false,
        title: '',
        width: '638px',
        id: '',
        parentId: ''
      },
      refreshTable: true,  /**重新渲染表格状态*/ 
      searchParams: { /**查询参数*/ 
        menuName: undefined,
        status: undefined
      },
      tableDataObj: {
        id: 'menuId',
        showSelection: false,
        defaultExpandAll: true,
        treeProps: {children: 'children', hasChildren: 'hasChildren'},
        option: [
          {id: 1, label: '菜单名称', prop: 'menuName', show: 1, needSlot: false},
          {id: 2, label: '图标', prop: 'icon', show: 1, needSlot: true},
          {id: 3, label: '排序', prop: 'orderNum', show: 1, needSlot: false},
          {id: 4, label: '权限标识', prop: 'perms', show: 1, needSlot: false},
          {id: 5, label: '组件路劲', prop: 'component', show: 1, needSlot: false},
          {id: 6, label: '状态', prop: 'status', show: 1, needSlot: true},
          {id: 7, label: '创建时间', prop: 'createTime', width: 160, show: 0, needSlot: false}
        ]
      },
    };
  },
  async created() {
    await this.getSysShowHide();
    await this.getSysNormalDisable();
    this.getListFn();
  },
  methods: {
    async getListFn() {  /** 查询菜单列表 */
      this.loading = true;
      const res = await listMenu(this.searchParams);
      if(res.code == 200) {
        this.tableData = this.handleTree(res.data, "menuId");
      }
      setTimeout(() => {
        this.loading = false;
      }, 500);
    },
    handleSearch() {  /** 搜索按钮操作 */
      this.getListFn();
    },
    handleReset() { /** 重置按钮操作 */
      this.resetForm("searchParams", ()=>{
        this.handleSearch();
      });
    },
    handleAdd(row) { /** 新增按钮操作 */
      this.dialogObj.visible = true;
      this.dialogObj.id = '';
      if (row != null && row.menuId) {
        this.dialogObj.parentId = row.menuId;
      } else {
        this.dialogObj.parentId = 0;
      }
      this.dialogObj.title = "添加菜单";
    },
    toggleExpandAll() { /** 展开/折叠操作 */
      this.refreshTable = false;
      this.tableDataObj.defaultExpandAll = !this.tableDataObj.defaultExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    handleEdit(row) { /** 修改按钮操作 */
      this.dialogObj.id = row.menuId;
      this.dialogObj.parentId = '';
      this.dialogObj.title = "修改菜单";
      this.dialogObj.visible = true;
    },
    handleDelete(row) { /** 删除按钮操作 */
      this.$modal.confirm('是否确认删除名称为"' + row.menuName + '"的数据项？').then(async ()=> {
        this.loading = true;
        const res= await delMenu(row.menuId);
        if(res.code == 200) {
          this.getListFn();
          this.$modal.msgSuccess("删除成功");
        }
        setTimeout(() => {
          this.loading = false;
        }, 500);
      }).catch(() => {});
    }
  }
};
</script>
